package com.example.teamtribe.utils;

import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;

import com.example.teamtribe.Application;

import java.io.ByteArrayOutputStream;
import java.io.File;

public class ImageUtil {

    public static String encodeToString(byte[] bytes) {
        return Base64.encodeToString(bytes, Base64.DEFAULT);
    }

    public static byte[] decode(String str) {
        return Base64.decode(str, Base64.DEFAULT);
    }

    public static byte[] toBytes(Bitmap bitmap){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    public static File getFile(Uri uri) {
        String path = "";
        Cursor cursor = Application.getContext().getContentResolver().query(uri, null, null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                Log.d("真的地址", path);
            }
            cursor.close();
        }
        return new File(path);
    }
}
